home *** CD-ROM | disk | FTP | other *** search
/ Power Tools for Macintosh / Power Tools for Macintosh (SoftBit)(1992).iso / Applications / Alpha 4.01 / ACMDS / StringToC.c < prev    next >
C/C++ Source or Header  |  1990-08-14  |  5KB  |  175 lines

  1. /****************************************************************
  2. *                                                               *
  3. * StringToC - convert a string to a C version of the string     *
  4. *                                                               *
  5. * Copyright © 1990 Karl J. Smith. All Rights Reserved           *
  6. *                                                               *
  7. * email: ksmith@jarthur.claremont.edu                           *
  8. *                                                               *
  9. * 8/14/90                                                       *
  10. *                                                               *
  11. * When debugging, set the project type to application,          *
  12. * and add the ANSI project.                                     *
  13. *                                                               *
  14. * When all your bugs are out, 'undef' DEBUG, set the project    *
  15. * type to Code Resource, with type = ACMD, name = <the name     *
  16. * of the function>, file type to 'AEXT', and the purgeable      *
  17. * flag set to true.                                             *
  18. *                                                               *
  19. * Also, you need to remove the ANSI project from the ACMD       *
  20. * project and replace it with the ANSI-A4 project if you use    *
  21. * any functions in ANSI, such as strlen.                        *
  22. *                                                               *
  23. * If you are not using THINK C, I'm not quite sure what you     *
  24. * should do. :-)                                                *
  25. *                                                               *
  26. ****************************************************************/
  27.  
  28. #undef    DEBUG
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32.  
  33. /* Prototypes */
  34. char *
  35. #ifdef    DEBUG
  36. dummy(char *inStr);
  37. #else    DEBUG
  38. main(char *inStr);
  39. #endif    DEBUG
  40.  
  41. char *
  42. #ifdef    DEBUG
  43. dummy(inStr)
  44. #else    DEBUG
  45. main(inStr)
  46. #endif    DEBUG
  47. char    *inStr;
  48. {
  49.     char *currentChar;            /* Current position in input string */
  50.     char *resultString;            /* String to return */
  51.     char *destination;            /* Current position in result string */
  52.     OSErr memError;                /* Error, if any */
  53.     
  54.     Size returnLength;            /* Length of string to return */
  55.  
  56.     resultString = inStr;        /* In case the length is 0 */
  57.     returnLength = GetPtrSize(resultString);
  58.     
  59.     if (returnLength != 0)
  60.     {
  61.         resultString = NewPtr(returnLength);
  62.         if (resultString != NULL)
  63.         {
  64.             destination = resultString;
  65.             for (currentChar = inStr;*currentChar != 0;currentChar++)
  66.             {
  67.             
  68.                 if (    (*currentChar == '\n') || (*currentChar == '\t')
  69.                     ||    (*currentChar == '\v') || (*currentChar == '\b')
  70.                     ||    (*currentChar == '\r') || (*currentChar == '\f')
  71.                     ||    (*currentChar == '\a') || (*currentChar == '\a')
  72.                     ||    (*currentChar == '\\') || (*currentChar == '\'')
  73.                     ||    (*currentChar == '\"')   )
  74.                     
  75.                     {
  76.                         SetPtrSize(resultString, ++returnLength);
  77.                         
  78.                         if (MemError() == 0)
  79.                             switch (*currentChar)
  80.                             {
  81.                                 case '\n': *destination++ = '\\'; *destination++ = 'n';
  82.                                     break;
  83.                                 case '\t': *destination++ = '\\'; *destination++ = 't';
  84.                                     break;
  85.                                 case '\v': *destination++ = '\\'; *destination++ = 'v';
  86.                                     break;
  87.                                 case '\b': *destination++ = '\\'; *destination++ = 'b';
  88.                                     break;
  89.                                 case '\r': *destination++ = '\\'; *destination++ = 'r';
  90.                                     break;
  91.                                 case '\f': *destination++ = '\\'; *destination++ = 'f';
  92.                                     break;
  93.                                 case '\a': *destination++ = '\\'; *destination++ = 'a';
  94.                                     break;
  95.                                 case '\\': *destination++ = '\\'; *destination++ = '\\';
  96.                                     break;
  97.                                 case '\'': *destination++ = '\\'; *destination++ = '\'';
  98.                                     break;
  99.                                 case '\"': *destination++ = '\\'; *destination++ = '\"';
  100.                                     break;
  101.                                 default:  
  102.                                     break;
  103.                             }
  104.                         else
  105.                             {
  106.                                 SysBeep(5);
  107.                                 DisposPtr(resultString);
  108.                                 return(inStr);
  109.                             }
  110.                     }
  111.                 else        /* It's just an ordinary character */
  112.                     {
  113.                         *destination++ = *currentChar;    /* Just copy the character */
  114.                     }                
  115.             }
  116.             *destination = 0;    /* Terminate the string */
  117.             DisposPtr(inStr);
  118.         }
  119.         else    /* We don't have enough memory */
  120.         {
  121.             SysBeep(5);
  122.             return(inStr);
  123.         }
  124.     }
  125.     return(resultString);
  126. }
  127.  
  128.  
  129. #ifdef    DEBUG
  130.  
  131. char * ConvertRtoN(char *theStr);
  132.  
  133. char * ConvertRtoN(theStr)
  134.     char *theStr;
  135. {
  136.     char *currentChar;
  137.     
  138.     for (currentChar = theStr;*currentChar != 0; currentChar++)
  139.         if (*currentChar == '\r')
  140.             *currentChar = '\n';
  141. }
  142.     
  143. main()
  144. {
  145.     char    *str = "Boing\rMonkey\r\n\t\v\b\f\a\\\'\"";
  146.     char    *ret;
  147.     char    *once;
  148.     char    *currentChar;
  149.         
  150.     once = NewPtr(256L);
  151.     
  152.     strcpy(once, str);                
  153.     
  154.     ret = dummy(once);
  155.     
  156.     ConvertRtoN(ret);                /* Convert '\r' to '\n' so we can see them in a printf */
  157.     ConvertRtoN(str);
  158.         
  159.     printf("The original is:\n\n%s", str);
  160.     printf("\n----\n\nThe result is:\n\n");
  161.     
  162.     for(currentChar = ret;*currentChar != 0;currentChar++)
  163.         {
  164.             printf("%c", *currentChar);
  165.         }
  166.     
  167. }
  168. #endif    DEBUG
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.